package cn.butcher21.view;

import cn.butcher21.dao.SaleDao;
import cn.butcher21.pojo.Sale;
import cn.butcher21.pojo.SaleData;
import cn.butcher21.print.Goods;
import cn.butcher21.print.Printer;
import cn.butcher21.print.SalesTicket;
import cn.butcher21.utils.BuildScene;
import cn.butcher21.utils.Pay;
import cn.butcher21.utils.ToPrintList;
import cn.butcher21.window.SuccessWindow;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CusScanController {

    @FXML
    private ImageView payCode;

    @FXML
    private Label cusPayTips;
    LoginController login = (LoginController) Context.rootController.get(LoginController.class.getSimpleName());
    SaleController sale = (SaleController) Context.rootController.get(SaleController.class.getSimpleName());


    @FXML
    private void initialize() {
        File file = new File("src/main/resources/payCode.png");
        String string = file.toURI().toString();
        Image image = new Image(string);
        payCode.setImage(image);
        cusPayTips.setText("请您打开支付宝支付" + sale.getTotal().getText() + "元");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Pay pay = new Pay();
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (pay.query(sale.sId)) {
                        System.out.println("支付成功啦！！！");
                        List<Goods> format = ToPrintList.format(sale.saleDatas);
                        SalesTicket stk = new SalesTicket(format, login.getUsername().getText(), sale.sId, String.valueOf(format.size()), sale.getTotal().getText(), sale.getTotal().getText(), "0", new Date());
                        new Printer(stk).printer();
                        SaleDao saleDao = new SaleDao();
                        try {
                            saleDao.add(sale.salesToDB);
                            for (Sale sale1 : sale.salesToDB) {
                                String gid = sale1.getGid();
                                int count = sale1.getCount();
                                saleDao.reduce(gid, count);
                            }
                            sale.sId = "";
                            sale.saleDatas.clear();

                            sale.salesToDB.clear();
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    BuildScene.stages.get("cusStage").close();
                                    new SuccessWindow();
                                }
                            });
                            SaleController o = (SaleController) Context.rootController.get(SaleController.class.getSimpleName());
                            o.loadSaleTableData();
                        } catch (SQLException e) {
                            e.printStackTrace();
                            sale.saleDatas.add(new SaleData());
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        break;
                    } else {
                        System.out.println("还未支付。。。");
                    }
                }

            }
        }).start();
    }


}
